<template>
  <div>
    <div id="main" style="width: 500px; height: 500px"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { mapActions, mapState } from 'vuex'
export default {
  name: 'myEcharts',
  data() {
    return {
      titleArr: [],
      priceArr: [],
    }
  },
  async created() {},
  async mounted() {
    await this.fetchCartList()
    this.homeList.forEach((item) => {
      this.titleArr.push(item.goodsName)
      this.priceArr.push(item.price)
      // 异步数据，等待数据过来之后，才渲染
      this.drawEcharts()
    })
  },
  computed: {
    ...mapState(['homeList']),
  },
  methods: {
    ...mapActions(['fetchCartList']),
    drawEcharts() {
      // 基于准备好的dom，初始化echarts实例
      const myChart = echarts.init(document.getElementById('main'))
      // 绘制图表
      myChart.setOption({
        title: {
          text: 'ECharts 入门示例',
        },
        tooltip: {},
        xAxis: {
          data: this.titleArr,
        },
        yAxis: {},
        series: [
          {
            name: '价格',
            type: 'bar',
            data: this.priceArr,
          },
        ],
      })
    },
  },
}
</script>

<style></style>
